package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {

	loopFunc()

	time.Sleep(time.Second)
}

func loopFunc() {

	lock := sync.Mutex{}

	for i := 0; i < 3; i++ {
		go func(i int) {
			// defer 会在函数退出的时候执行，所以每次循环执行完，锁一定会被释放
			lock.Lock()
			defer lock.Unlock()
			fmt.Println("loopFunc:", i)
		}(i)
	}
}
